// Exploring F# and .net libraries by example

open System;;
let convertDataRow(str:string) = 
  let cells  = List.ofSeq(str.Split(','))
  match cells with 
    | lbl::num::_ -> 
      let numI = Int32.Parse(num) 
      (lbl, numI)
    | _ -> failwith "Incorrect data format!"
    ;;

convertDataRow("Testing reading,1234")

String.Concat("1 + 3",3)

String.Join(", ", [| "1"; "2"; "3" |])
"hello".Length

let str = "hello"
str.[str.Length - 1]


let rec processLines(lines) = 
	match lines with 
	| [] -> []
	| st::tail ->
		let row = convertDataRow(st)
		let rest = processLines(tail)
		row :: rest
		
let tst = processLines ["Test1,123"; "Test2,456"]

let rec countSum(rows) = 
	match rows with
	| [] -> 0
	| (_, n)::tail ->
		let sumRest = countSum(tail)
		n + sumRest
		
let sum = countSum tst
100.0 / float(sum) * 123.0

//

open System.IO

let lines = List.ofSeq(File.ReadAllLines(@"C:\dev\Scratch\jamest-fsharp-playground\RWFP\data.csv"))

let data = processLines(lines)

let sum = float(countSum(data))

for (lbl, num) in data do 
	let perc = int((float(num)) / sum * 100.0)
	Console.WriteLine("{0,-18} - {1,8} ({2}%)", lbl, num, perc)
	
	

	
open System
open System.Drawing
open System.Windows.Forms

let main = new Form(Width = 620, Height = 450, Text = "Pie Chart")

let menu = new ToolStrip()
let btnOpen = new ToolStripButton("Open")
let btnSave = new ToolStripButton("Save", Enabled = false)
menu.Items.Add(btnOpen)
menu.Items.Add(btnSave)

let img = new PictureBox (BackColor = Color.White, Dock = DockStyle.Fill, SizeMode = PictureBoxSizeMode.CenterImage)

main.Controls.Add(menu)
main.Controls.Add(img)

[<STAThread>]
do 
		Application.Run(main)